package com.ywgt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ywgt.pojo.*;
import com.ywgt.mapper.GoodsMapper;
import com.ywgt.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ywgt.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 * 商品基本信息表 服务实现类
 * </p>
 *
 * @author yiping
 * @since 2021-03-13
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    @Autowired
    BrandService brandService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    GoodsAttributeService goodsAttributeService;

    @Autowired
    GoodsSpecificationService goodsSpecificationService;

    @Autowired
    GoodsProductService goodsProductService;
    @Override
    public Object catAndBrand() {

        List<Brand> brands = brandService.list();
        List<Map<String, Object>> brandList = new ArrayList<>(brands.size());
        for (Brand brand : brands) {
            Map<String,Object> br=new HashMap<>();
            br.put("value",brand.getId());
            br.put("label",brand.getName());
            brandList.add(br);
        }
        List<Category> categories = categoryService.getMenu();
        List<CategoryVo> categoryList = new ArrayList<>(categories.size());
        for (Category category : categories) {
            CategoryVo categoryVo=new CategoryVo();
            categoryVo.setValue(category.getId());
            categoryVo.setLabel(category.getName());
            List<Category> child = category.getChild();
            List<CategoryVo> voChild=new ArrayList<>();
            for (Category chi : child) {
                CategoryVo cvo=new CategoryVo();
                cvo.setValue(chi.getId());
                cvo.setLabel(chi.getName());
                voChild.add(cvo);
            }
            categoryVo.setChildren(voChild);
            categoryList.add(categoryVo);
        }
        Map<String,Object> data=new HashMap<>();
        data.put("brandList",brandList);
        data.put("categoryList",categoryList);
        return ResponseUtil.ok(data);
    }

    @Override
    public Page<Goods> getGoods(String name, String goodsSn, int page, int limit, String sort, String order) {
        PageHelper.startPage(page,limit);
        return baseMapper.selectGoods(name,goodsSn,sort,order);
    }

    @Override
    public Object create(GoodsAllinone goodsAllinone) {
       baseMapper.insert(goodsAllinone.getGoods());
        GoodsAttribute[] attributes = goodsAllinone.getAttributes();
        GoodsProduct[] products = goodsAllinone.getProducts();
        GoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        for (GoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsAllinone.getGoods().getId());
        }
        for (GoodsProduct product : products) {
            product.setGoodsId(goodsAllinone.getGoods().getId());
        }
        for (GoodsSpecification specification : specifications) {
            specification.setGoodsId(goodsAllinone.getGoods().getId());
        }
       goodsAttributeService.saveBatch(Arrays.asList(attributes));
       goodsSpecificationService.saveBatch(Arrays.asList(specifications));
       goodsProductService.saveBatch(Arrays.asList(products));
       return ResponseUtil.ok();
    }
    @Transactional
    @Override
    public Object updateGoods(GoodsAllinone goodsAllinone) {
        baseMapper.updateById(goodsAllinone.getGoods());
        Map<String,Object> delMap=new HashMap<>();
        delMap.put("goods_id",goodsAllinone.getGoods().getId());
        goodsSpecificationService.removeByMap(delMap);
        goodsAttributeService.removeByMap(delMap);
        goodsProductService.removeByMap(delMap);
        GoodsAttribute[] attributes = goodsAllinone.getAttributes();
        GoodsProduct[] products = goodsAllinone.getProducts();
        GoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        for (GoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsAllinone.getGoods().getId());
        }
        for (GoodsProduct product : products) {
            product.setGoodsId(goodsAllinone.getGoods().getId());
        }
        for (GoodsSpecification specification : specifications) {
            specification.setGoodsId(goodsAllinone.getGoods().getId());
        }
        goodsProductService.saveBatch(Arrays.asList(products));
        goodsAttributeService.saveBatch(Arrays.asList(attributes));
        goodsSpecificationService.saveBatch(Arrays.asList(specifications));
        return ResponseUtil.ok();
    }

    @Override
    public Object detail(Integer id) {
        Map<String,Object> searchMap=new HashMap<>();
        searchMap.put("goods_id",id);
        List<GoodsProduct> products = goodsProductService.listByMap(searchMap);
        List<GoodsSpecification> specifications = goodsSpecificationService.listByMap(searchMap);
        List<GoodsAttribute> attributes = goodsAttributeService.listByMap(searchMap);
        Goods goods = baseMapper.selectById(id);
        Category category = categoryService.getById(goods.getCategoryId());
        Integer[] ids=new Integer[2];
        if(category!=null){
            ids=new Integer[]{category.getPid(),category.getId()};
        }
        Map<String,Object> data=new HashMap<>();
        data.put("goods", goods);
        data.put("specifications", specifications);
        data.put("products", products);
        data.put("attributes", attributes);
        data.put("categoryIds", ids);
        return ResponseUtil.ok(data);
    }

    @Override
    public List<Goods> getNewGoods(int i, Integer newLimit) {
        LambdaQueryWrapper<Goods> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getIsNew,1);
        wrapper.eq(Goods::getDeleted,0);
        wrapper.eq(Goods::getIsOnSale,1);
        wrapper.orderByDesc(Goods::getBrowse);
        PageHelper.startPage(i,newLimit);
        return this.list(wrapper);
    }
    @Override
    public List<Goods> getHotGoods(int i,Integer hotLimit){
        LambdaQueryWrapper<Goods> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getIsHot,1);
        wrapper.eq(Goods::getDeleted,0);
        wrapper.eq(Goods::getIsOnSale,1);
        wrapper.orderByDesc(Goods::getBrowse);
        PageHelper.startPage(i,hotLimit);
        return this.list(wrapper);
    }

    @Override
    public List<Goods> getByCategorys(List<Integer> ids, int i, Integer catlogListLimit) {
        LambdaQueryWrapper<Goods> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Goods::getCategoryId,ids);
        PageHelper.startPage(i,catlogListLimit);
        return this.list(wrapper);
    }

    @Override
    public Page<Goods> getGoodsByCondition(Integer categoryId, Integer brandId, String keyword, Boolean isHot, Boolean isNew, Integer page, Integer size, String sort, String order) {
        PageHelper.startPage(page,size);
        return baseMapper.selectByCondition(categoryId,brandId,keyword,isHot,isNew,sort,order);
    }

    @Override
    public List<Integer> getCategoryIds(Integer brandId, String keyword, Boolean isHot, Boolean isNew) {
        List<Integer> ids=new ArrayList<>();
        List<Goods> categories=baseMapper.selectCateGoryIds(brandId,keyword,isHot,isNew);
        if(categories!=null) {
            categories.stream().distinct().forEach(a -> ids.add(a.getCategoryId()));
        }
        return ids;
    }

    @Override
    public void incrBrowser(Integer id) {
        baseMapper.incry(id);
    }


}
